package org.liang.code.rabbitMQ.主题模式;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.liang.code.rabbitMQ.utils.RabbitConstant;
import org.liang.code.rabbitMQ.utils.RabbitUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Random;
import java.util.concurrent.TimeoutException;

public class WeatherProducer {

    private static final Logger logger = LoggerFactory.getLogger(WeatherProducer.class);

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {

        try (
                Connection connection = RabbitUtils.getConnection();
                Channel channel = connection.createChannel()
        ) {
            channel.exchangeDeclare(RabbitConstant.EXCHANGE_WEATHER_TOPIC, "topic", true);

            channel.queueDeclare(RabbitConstant.QUEUE_BEIJING, true, false, false, null);

            channel.queueBind(RabbitConstant.QUEUE_BEIJING, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "city.Beijing.#");

            channel.queueDeclare(RabbitConstant.QUEUE_PARIS, true, false, false, null);
            channel.queueBind(RabbitConstant.QUEUE_PARIS, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "city.Paris.*");

            Random random = new Random();

            String[] cities = { "Beijing", "New York", "Tokyo", "Paris", "London" };
            String[] countries = { "China", "USA", "Japan", "France", "UK" };
            String[] weathers = { "Sunny", "Rainy", "Cloudy", "Snowy" };

            while (true) {
                String weather = weathers[random.nextInt(weathers.length)];
                int maxTemp, minTemp;
                switch (weather) {
                    case "Sunny":
                        maxTemp = random.nextInt(11) + 25; // 25-35℃
                        minTemp = maxTemp - 10; // 日温差约10℃
                        break;
                    case "Rainy":
                        maxTemp = random.nextInt(6) + 20; // 20-25℃
                        minTemp = random.nextInt(10) + 10; // 10-20℃
                        break;
                    case "Cloudy":
                        maxTemp = random.nextInt(9) + 20; // 20-28℃
                        minTemp = random.nextInt(8) + 15; // 15-22℃
                        break;
                    default: // Snowy
                        maxTemp = random.nextInt(6) - 5; // -5~0℃
                        minTemp = random.nextInt(6) - 10; // -10~-5℃
                }
                int idx = random.nextInt(countries.length);
                String json = String.format(
                        "{\"date\":\"%s\", \"country\":\"%s\", \"city\":\"%s\", \"weather\":\"%s\", " +
                                "\"minTemp\":%d, \"maxTemp\":%d}",
                        LocalDate.now(),
                        countries[idx],
                        cities[idx],
                        weather,
                        minTemp,
                        maxTemp);
                String routingKey = "city." + cities[idx] + ".weather";
                logger.info("路由键：{}，天气信息：{}", routingKey, json);

                channel.basicPublish(RabbitConstant.EXCHANGE_WEATHER_TOPIC, routingKey, null, json.getBytes());
                Thread.sleep(1000);
            }
        }
    }
}
